package zzjson.rabbitmq.message;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import zzjson.rabbitmq.BaseRabbitMq;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.rabbitmq.message</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年04月25日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class MessageProducer {

	public static void main(String[] args) throws IOException {
		Connection conn = BaseRabbitMq.getConn();
		Channel channel = conn.createChannel();
		Map<String, Object> headers = new HashMap<String, Object>();
		headers.put("name", "zzjson");
		headers.put("level", "top");

		AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
				.deliveryMode(2)   // 只有2代表持久化
				.contentEncoding("UTF-8")  // 编码
				.expiration("10000")  // TTL，过期时间
				.headers(headers) // 自定义属性
				.priority(5) // 优先级，默认为5，配合队列的 x-max-priority 属性使用
				.messageId(String.valueOf(UUID.randomUUID()))
				.build();

		String msg = "Hello world, Rabbit MQ";

		// 发送消息
		// String exchange, String routingKey, BasicProperties props, byte[] body
		channel.basicPublish("", "ORIGIN_QUEUE", properties, msg.getBytes());

		BaseRabbitMq.close(channel, conn);

	}
}